/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.aliyuncs.AliyuncsUtil;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdv;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategory;
import com.thinkgem.jeesite.modules.hk.sys.service.adv.HkCategoryService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdv;
import com.thinkgem.jeesite.modules.hk.adv.service.HkMediaAdvService;

import java.util.List;

/**
 * 自媒体广告Controller
 * @author 罗天文
 * @version 2018-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/adv/hkMediaAdv")
public class HkMediaAdvController extends BaseController {

	@Autowired
	private HkMediaAdvService hkMediaAdvService;
	@Autowired
	private HkCategoryService hkCategoryService;
	@ModelAttribute
	public HkMediaAdv get(@RequestParam(required=false) String id) {
		HkMediaAdv entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkMediaAdvService.get(id);
		}
		if (entity == null){
			entity = new HkMediaAdv();
		}
		return entity;
	}
	
	@RequiresPermissions("adv:hkMediaAdv:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkMediaAdv hkMediaAdv, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkMediaAdv> page = hkMediaAdvService.findPage(new Page<HkMediaAdv>(request, response), hkMediaAdv);
		if(null!=page.getList()) {
			StringBuffer sb=new StringBuffer();
			for (HkMediaAdv hkEnterpriseAdv2 : page.getList()) {
				sb.setLength(0);
				if(StringUtils.isNotEmpty(hkEnterpriseAdv2.getCategory())) {
					for (String id : hkEnterpriseAdv2.getCategory().split(",")) {
						String name=(String)UserUtils.getCache("id");
						if(StringUtils.isEmpty(name)){
							HkCategory hkCategory=hkCategoryService.get(id);
							if(null!=hkCategory){
								UserUtils.putCache(hkCategory.getId(),hkCategory.getName());
								name=hkCategory.getName();
							}
						}
						if(StringUtils.isNotEmpty(name)) {
							sb.append(name).append(",");
						}

					}
					hkEnterpriseAdv2.setCategory(sb.toString());
				}


			}
		}
		model.addAttribute("page", page);
		return "hk/adv/hkMediaAdvList";
	}

	@RequiresPermissions("adv:hkMediaAdv:view")
	@RequestMapping(value = "form")
	public String form(HkMediaAdv hkMediaAdv, Model model) {
		model.addAttribute("hkMediaAdv", hkMediaAdv);
		model.addAttribute("areas", UserUtils.getAreaList());
		HkCategory hkCategory=new HkCategory();
		hkCategory.setType("1");
		List<HkCategory> categorys = hkCategoryService.findList(hkCategory);
		model.addAttribute("categorys", categorys);
//		model.addAttribute("countrys", UserUtils.getAreaListByParentId("0"));
//		if(null!=hkMediaAdv.getProvinceId()){
//			model.addAttribute("provinces",UserUtils.getAreaListByParentId(hkMediaAdv.getCountryId()));
//		}
//		if(null!=hkMediaAdv.getCityId()){
//			model.addAttribute("citys",UserUtils.getAreaListByParentId(hkMediaAdv.getProvinceId()));
//		}
		return "hk/adv/hkMediaAdvForm";
	}

	@RequiresPermissions("adv:hkMediaAdv:edit")
	@RequestMapping(value = "pass")
	public String pass(HkMediaAdv hkMediaAdv, Model model, RedirectAttributes redirectAttributes) {
		hkMediaAdvService.pass(hkMediaAdv);
		addMessage(redirectAttributes, "审核自媒体广告成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkMediaAdv/?repage";
	}
    @RequiresPermissions("adv:hkMediaAdv:edit")
    @RequestMapping(value = "nopass")
    public String nopass(HkMediaAdv hkMediaAdv, Model model, RedirectAttributes redirectAttributes) {
        hkMediaAdvService.nopass(hkMediaAdv);
        addMessage(redirectAttributes, "不通过自媒体广告成功");
        return "redirect:"+Global.getAdminPath()+"/adv/hkMediaAdv/?repage";
    }

	@RequiresPermissions("adv:hkMediaAdv:edit")
	@RequestMapping(value = "save")
	public String save(HkMediaAdv hkMediaAdv, Model model, RedirectAttributes redirectAttributes) throws Exception {
		if (!beanValidator(model, hkMediaAdv)){
			return form(hkMediaAdv, model);
		}
		hkMediaAdvService.save(hkMediaAdv);
		addMessage(redirectAttributes, "保存自媒体广告成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkMediaAdv/?repage";
	}
	
	@RequiresPermissions("adv:hkMediaAdv:edit")
	@RequestMapping(value = "delete")
	public String delete(HkMediaAdv hkMediaAdv, RedirectAttributes redirectAttributes) {
		hkMediaAdvService.delete(hkMediaAdv);
		addMessage(redirectAttributes, "删除自媒体广告成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkMediaAdv/?repage";
	}

	@RequestMapping(value = "mediaPlayPath")
	public String mediaPlayPath(){

		return "/hk/adv/mediaPlay";
	}

	/**
	 * 获取播放凭证
	 * @return
	 */
	@RequestMapping(value = "getPlayInfoAuth")
	@ResponseBody
	public String getPlayInfoAuth(HttpServletRequest request,Model model){
		String videoId  = request.getParameter("videoId");
		HkMediaAdv hkMediaAdv = new HkMediaAdv();
		hkMediaAdv.setImgSrc(videoId);
		String response  = AliyuncsUtil.requestServer(hkMediaAdv,true);
		return response;
	}

}